***IRP estimates for figures 1-4***
use *idnr *earn* *yob *obsy *max* year woman if year>=1985 using "use\swepanel_zeros", replace
g agec1=year-yob-40
g agef1=fobsy-fyob
g agem1=mobsy-myob
egen fmage1=rowmean(agef1 agem1)
g maxage1=agef1
replace maxage1=agem1 if missing(fearn) & !missing(mearn)
replace maxage1=agem1 if mearn>fearn & !missing(mearn) & !missing(fearn)
forval i=2/4{
	g agec`i'=agec1^`i'
	g agef`i'=agef1^`i'
	g agem`i'=agem1^`i'
	g fmage`i'=fmage1^`i'
	g maxage`i'=maxage1^`i'
}

*Figure 1: sons and daughters pooled on average parental income
global fig "1"
global inc "pearn"
global incp "pfmearn"
global agep "fmage"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.year#c.$incp i.year $agep* agec* c.agec*#c.$incp, cluster(idnr)
levelsof year, local(years)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local years{
	sca t1=_se[`i'.year#c.$incp]
	mat $se=$se\t1
	sum $inc if year==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if year==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
*Figure 2a: sons on average parental income
global fig "2s"
global inc "pearn"
global incp "pfmearn"
global agep "fmage"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.year#c.$incp i.year $agep* agec* c.agec*#c.$incp if woman==0, cluster(idnr)
levelsof year, local(years)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local years{
	sca t1=_se[`i'.year#c.$incp]
	mat $se=$se\t1
	sum $inc if year==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if year==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
*Figure 2b: daughters on average parental income
global fig "2d"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.year#c.$incp i.year $agep* agec* c.agec*#c.$incp if woman==1, cluster(idnr)
levelsof year, local(years)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local years{
	sca t1=_se[`i'.year#c.$incp]
	mat $se=$se\t1
	sum $inc if year==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if year==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
*Figure 3a: sons and daughters pooled on fathers' income
global fig "3f"
global inc "pearn"
global incp "pfearn"
global agep "agef"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.year#c.$incp i.year $agep* agec* c.agec*#c.$incp, cluster(idnr)
levelsof year, local(years)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local years{
    sca t1=_se[`i'.year#c.$incp]
	mat $se=$se\t1
	sum $inc if year==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if year==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
*Figure 3b: sons and daughters pooled on mothers' income
global fig "3m"
global inc "pearn"
global incp "pmearn"
global agep "agem"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.year#c.$incp i.year $agep* agec* c.agec*#c.$incp, cluster(idnr)
levelsof year, local(years)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local years{
    sca t1=_se[`i'.year#c.$incp]
	mat $se=$se\t1
	sum $inc if year==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if year==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
*Figure 4a: sons on fathers' income
global fig "4fs"
global inc "pearn"
global incp "pfearn"
global agep "agef"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.year#c.$incp i.year $agep* agec* c.agec*#c.$incp if woman==0, cluster(idnr)
levelsof year, local(years)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local years{
    sca t1=_se[`i'.year#c.$incp]
	mat $se=$se\t1
	sum $inc if year==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if year==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
*Figure 4b: daughters on fathers' income
global fig "4fd"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.year#c.$incp i.year $agep* agec* c.agec*#c.$incp if woman==1, cluster(idnr)
levelsof year, local(years)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local years{
    sca t1=_se[`i'.year#c.$incp]
	mat $se=$se\t1
	sum $inc if year==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if year==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
*Figure 4c: sons on mothers' income
global fig "4ms"
global inc "pearn"
global incp "pmearn"
global agep "agem"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.year#c.$incp i.year $agep* agec* c.agec*#c.$incp if woman==0, cluster(idnr)
levelsof year, local(years)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local years{
    sca t1=_se[`i'.year#c.$incp]
	mat $se=$se\t1
	sum $inc if year==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if year==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
*Figure 4d: daughters on mothers' income
global fig "4md"
global beta b$fig /*matrix to store estimates*/
global se se$fig /*matrix to store standard errors*/
global var var$fig /*matrix to store variance of income measures*/
reg $inc i.year#c.$incp i.year $agep* agec* c.agec*#c.$incp if woman==1, cluster(idnr)
levelsof year, local(year)
mat $beta=e(b)'
mat $beta=$beta[1..r(r),....]
mat $se=(0)
mat $var=(0,0)
foreach i of local years{
	sca t1=_se[`i'.year#c.$incp]
	mat $se=$se\t1
	sum $inc if year==`i' & e(sample)==1
	sca t2=r(Var)
	sum $incp if year==`i' & e(sample)==1
	sca t3=r(Var)
	mat $var=$var\t2,t3
}
mat $se=$se[2...,....]
mat $var=$var[2...,....]
drop _all
set obs 35
g year=_n+1984
local est "1 2s 2d 3f 3m 4fs 4fd 4ms 4md"
foreach e of local est{
	svmat b`e'
	svmat se`e'
	svmat var`e'
	g sd`e'u=b`e'1+1.96*se`e'1
	g sd`e'l=b`e'1-1.96*se`e'1
}
rename (b11 b2s1 b2d1 b3f1 b3m1 b4fs1 b4fd1 b4ms1 b4md1) (irp1 irp2s irp2d irp3f irp3m irp4fs irp4fd irp4ms irp4md)
lab var irp1 "pooled/parental average"
lab var irp2s "son/parental average"
lab var irp2d "daughter/parental average"
lab var irp3f "pooled/father"
lab var irp3m "pooled/mother"
lab var irp4fs "son/father"
lab var irp4fd "daughter/father"
lab var irp4ms "son/mother"
lab var irp4md "daughter/mother"
lab var var11 "pooled/parental average"
lab var var2s1 "son/parental average"
lab var var2d1 "daughter/parental average"
lab var var3f1 "pooled/father"
lab var var3m1 "pooled/mother"
lab var var4fs1 "son/father"
lab var var4fd1 "daughter/father"
lab var var4ms1 "son/mother"
lab var var4md1 "daughter/mother"
lab var sd1u "pooled/parental average"
lab var sd2su "son/parental average"
lab var sd2du "daughter/parental average"
lab var sd3fu "pooled/father"
lab var sd3mu "pooled/mother"
lab var sd4fsu "son/father"
lab var sd4fdu "daughter/father"
lab var sd4msu "son/mother"
lab var sd4mdu "daughter/mother"
lab var sd1l "pooled/parental average"
lab var sd2sl "son/parental average"
lab var sd2dl "daughter/parental average"
lab var sd3fl "pooled/father"
lab var sd3ml "pooled/mother"
lab var sd4fsl "son/father"
lab var sd4fdl "daughter/father"
lab var sd4msl "son/mother"
lab var sd4mdl "daughter/mother"
compress
save "use\fig1-4_swe", replace
*Figure 1: sons and daughters pooled on parental average income
twoway (connect irp1 year, ms(o) lc(ebblue) mc(ebblue)) (rspike sd1u sd1l year, lc(ebblue)), ytitle("Rank persistence") xtitle("Year") graphr(fc(white) c(white)) legend(order(1) label(1 "Pooled sons and daughters")) ysc(r(0 0.3)) ylab(0(0.05)0.45) xsc(r(1985 2020)) xlab(1985(5)2020) saving("graphs\us_swe\irp1", replace)
graph export "graphs\us_swe\irp1.pdf", replace
graph export "graphs\us_swe\irp1.eps", replace
*Figure 2: sons and daughters separately on parental average income
twoway (connect irp2s year, ms(o) lc(ebblue) mc(ebblue)) (rspike sd2su sd2sl year, lc(ebblue)) (connect irp2d year, ms(t) lc(cranberry) mc(cranberry) lp(solid)) (rspike sd2du sd2dl year, lc(cranberry)), ytitle("Rank persistence") xtitle("Year") graphr(fc(white) c(white)) legend(order(1 3) label(1 "Sons") label(3 "Daughters")) ysc(r(0 0.3)) ylab(0(0.05)0.45) xsc(r(1985 2020)) xlab(1985(5)2020) saving("graphs\us_swe\irp2", replace)
graph export "graphs\us_swe\irp2.pdf", replace
graph export "graphs\us_swe\irp2.eps", replace
*Figure 3: sons and daughters pooled on fathers' and mothers' income
twoway (connect irp3f year, ms(o) lc(ebblue) mc(ebblue)) (rspike sd3fu sd3fl year, lc(ebblue)) (connect irp3m year, ms(t) lc(cranberry) mc(cranberry) lp(solid)) (rspike sd3mu sd3ml year, lc(cranberry)), ytitle("Rank persistence") xtitle("Year") graphr(fc(white) c(white)) legend(order(1 3) label(1 "Fathers") label(3 "Mothers")) ysc(r(0 0.3)) ylab(0(0.05)0.45) xsc(r(1985 2020)) xlab(1985(5)2020) saving("graphs\us_swe\irp3", replace)
graph export "graphs\us_swe\irp3.pdf", replace
graph export "graphs\us_swe\irp3.eps", replace
*Figure 4: sons and daughters separately on fathers' and mothers' income
twoway (connect irp4fs year, ms(o) lc(ebblue) mc(ebblue)) (rspike sd4fsu sd4fsl year, lc(ebblue)) (connect irp4fd year, ms(t) lc(cranberry) mc(cranberry) lp(solid)) (rspike sd4fdu sd4fdl year, lc(cranberry)), ytitle("Rank persistence") xtitle("Year") graphr(fc(white) c(white)) legend(order(1 3) label(1 "Sons") label(3 "Daughters")) ysc(r(0 0.3)) ylab(0(0.05)0.45) xsc(r(1985 2020)) xlab(1985(5)2020) saving("graphs\us_swe\irp4f", replace)
graph export "graphs\us_swe\irp4f.pdf", replace
graph export "graphs\us_swe\irp4f.eps", replace
twoway (connect irp4ms year, ms(o) lc(ebblue) mc(ebblue)) (rspike sd4msu sd4msl year, lc(ebblue)) (connect irp4md year, ms(t) lc(cranberry) mc(cranberry) lp(solid)) (rspike sd4mdu sd4mdl year, lc(cranberry)), ytitle("Rank persistence") xtitle("Year") graphr(fc(white) c(white)) legend(order(1 3) label(1 "Sons") label(3 "Daughters")) ysc(r(0 0.3)) ylab(0(0.05)0.45) xsc(r(1985 2020)) xlab(1985(5)2020) saving("graphs\us_swe\irp4m", replace)
graph export "graphs\us_swe\irp4m.pdf", replace
graph export "graphs\us_swe\irp4m.eps", replace

